<template>
<view>
<view class="container">
   <!-- 头部start -->
   <view class='userinfo_head3'>
     <view class='userinfo_head3_gray'></view>
     <view class='userinfo_head3_wrap'>
       <image class='userinfo_head3_bg' :src='loginUser.platformUser.headimgurl'></image>
       <block v-if="loginUser"> 
         <view class='userinfo_head3_content'>
           <image v-if="loginUser.platformUser.headimgurl" :src='loginUser.platformUser.headimgurl'  @tap='loginOut'></image>
           <image v-else src='https://image1.sansancloud.com/jianzhan/2017_09/24/16/08/58_464.jpg' @tap='loginOut'></image>
           <view class='userinfo_head3_name'>
               <text @tap='loginOut'>{{loginUser.platformUser.nickname}}</text>
               <view class='user_level' v-if="loginUser.platformUser.userLevelName" style='background:#fec746' data-link="user_level.html" @tap='tolinkUrl'>
                 <image class='user_level_img' src='https://image1.sansancloud.com/xianhua/2018_12/21/11/18/56_627.jpg'></image>
                 <view class='user_level_name'>{{loginUser.platformUser.userLevelName}}</view>
               </view>
           </view>
           
         </view>  
       </block>
       <block wx:else>
       <view class='userinfo_head3_content' @tap='login'>
           <image src='https://image1.sansancloud.com/jianzhan/2017_09/24/16/08/58_464.jpg'></image>
           <text>未登录</text>
         </view>
       </block>
     </view>
   </view>
   <view class='userShareEwm' @tap='showPoster'>
     <image class='userEwmCode' src='https://image1.sansancloud.com/xianhua/2019_1/4/20/42/14_861.jpg?x-oss-process=style/preview_120'></image>
   </view>
   <view class='userBindWxGz' @tap='clickWxGz'>
     <!-- <web-view src="https://www.baidu.com/"></web-view> -->
     <image class='userBindWxGzCode' v-if="!loginUser.platformUser.openid" src='https://image1.sansancloud.com/xianhua/2019_6/4/17/16/29_245.jpg?x-oss-process=style/preview_120'></image>
     <image class='userBindWxGzCode' v-else src='https://image1.sansancloud.com/xianhua/2019_8/5/16/30/58_518.jpg?x-oss-process=style/preview_120'></image>
   </view>
   <!-- 头部end -->
   <!-- <template is="userinfo_mendian" data="{{...userInfo,...serverData, loginUser:loginUser}}"></template>  -->
   <!-- <template is="userinfo_servant" data="{{...userInfo,...servantData, loginUser:loginUser}}"></template>  -->
   <!-- 角色选择start -->
   <view class='userinfo_mendian' v-if="curPersonnelType!=-1">
     <view class='userinfo_mendian_left' :data-link='havaMoreSelectType[curPersonnelType].linkUrl'  @tap='tolinkUrl'>
       <image mode='aspectFit' :src="havaMoreSelectType[curPersonnelType].iconPath"></image>
       <view  class="userinfo_mendian_text" >{{havaMoreSelectType[curPersonnelType].text}} </view>
     </view>
     <!-- <image class='more_select' src='https://image1.sansancloud.com/aikucun/2018_03/22/11/28/36_332.jpg?x-oss-process=style/preview_120'></image> -->
     <view>
       <picker style='padding:20rpx' @change="bindPickerChange" :range="havaMoreSelectTypeArr">
         <view class='pickerRight'>
           <image class='pickerImg' src='https://image1.sansancloud.com/aikucun/2018_03/22/11/28/36_332.jpg?x-oss-process=style/preview_120'></image>
         </view>
       </picker >
       <!-- <image class='more_select' src='https://image1.sansancloud.com/aikucun/2018_03/22/11/28/36_332.jpg?x-oss-process=style/preview_120'></image> -->
     </view>
   </view>
   <!-- <component is="userinfo_servant" error-data="...userInfo,havaMoreSelectTypeArr,havaMoreSelectType, curPersonnelType,loginUser:loginUser"></component> -->
   <!-- 角色选择end -->
   <!-- 订单菜单-栏目 -->
  <block v-if="!componentState">
	<view class='userinfo_order3_container'>
	  <view class='userinfo_order3_topView' data-link='order_list_0.html?easyStatusName=全部订单&easyStatus=0'  @tap='tolinkUrl'>
	    <view class='font_08'>我的订单</view>
	    <text class='font_08'>全部订单 ></text> 
	  </view>
	  <view  class="userinfo_order3_list_container" >
	      <view class="userinfo_order3_list_item font_08" style="width:25%"  v-for="(cells,index) in orderData.cells" :key="index" :data-link='cells.linkUrl' @tap='tolinkUrl'>
	        
	        <view class='imageContainer_order'>
	          <view class='red_poient_num' v-if='cells.showCountNum > 0'>{{cells.showCountNum}}</view>
	          <image  :src="cells.iconPath+'?imageMogr2/thumbnail/400x/interlace/0'"></image>
	        </view>
	        <view class='userinfo_order3_list_item_text'  :site_url="cells.linkUrl"  :targetType="cells.targetType">
	          {{cells.text}}
	        </view>
	      </view>
	  </view> 
	</view>
	<view  class="userinfo_list">
	    <view class="userinfo_list_item font_08"  v-for="(cells,index) in ListData.cells" :key="index" :data-link='cells.linkUrl' @tap='tolinkUrl'>
	      <view class='userinfo_list_item_left'>
	        <image style="" :src="cells.iconPath+'?imageMogr2/thumbnail/120x/interlace/0'"></image> 
	        <view class='userinfo_list_item_text'  :site_url="cells.linkUrl"  :targetType="cells.targetType">
	          {{cells.text}}
	        </view>
	      </view>
	      <image class='' src='https://image1.sansancloud.com/jianzhan/2018_9/11/13/41/53_722.jpg?x-oss-process=style/preview_120'></image>
	
	    </view>
	</view>
    <!-- <component is="userinfo_order3" error-data="...userInfo,...orderData, loginUser:loginUser"></component> 
    <component is="userinfo_list" error-data="...userInfo,...ListData, loginUser:loginUser"></component> -->
  </block>
  <!-- 订单菜单-栏目 -->
  <block v-else>
    <custom-page-index-component :receiveData="sendCustomPageData" v-if="sendCustomPageData"></custom-page-index-component>
  </block>
   <footer-component v-if="setting.platformSetting.jishuzhichi"></footer-component> 
   <posters-user-info-component :ewmImgUrl="ewmImgUrl" v-if="posterState" @closePoaster="getChilrenPoster"></posters-user-info-component>
</view>
<!-- 授权窗口 -->
  <view v-if="showPopup">
  <!-- authorizationCount==1&& -->
    <view class="authorizationBg"></view>
    <view class="authorization">
      <view class="authorizationPopup">
        <view class="tipContent">
          <text style="font-size:38rpx;">前往微信授权</text>
        </view>
        <view class="buttonArray">
          <button class="button cancel" @tap="cancel">取消</button>
          <button class="button confirm" open-type="getUserInfo" :style="'color:' + setting.platformSetting.defaultColor" @getuserinfo="bindGetUserInfo">确定</button>
        </view>
      </view>
    </view>
  </view>
</view>
</template>

<script>

export default {
  data() {
    return {
      setting: null,
      userData: null,
      PaiXuPartials: null,
      loginUser: null,
      componentState: true,
	  sendCustomPageData:null,
      //组件的data
      curPersonnelType: -1,
      havaMoreSelectType: [],
      havaMoreSelectTypeArr: [],
      // headData:null,
      showPopup: false,
      blankData: {
        color: "rgb(244,244,244)",
        height: 12
      },
      orderData: {
        cells: [{
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/47/6_417.jpg",
          linkUrl: "order_list_2.html?easyStatus=2&easyStatusName=待付款",
          text: "待付款",
          color: "#777777",
          showCountNum: 0
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/45/21_802.jpg",
          linkUrl: "order_list_3.html?easyStatus=3&easyStatusName=待发货",
          text: "待发货",
          color: "#777777",
          showCountNum: 0
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/45/21_775.jpg",
          linkUrl: "order_list_4.html?easyStatus=4&easyStatusName=待收货",
          text: "待收货",
          color: "#777777",
          showCountNum: 0
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/45/22_291.jpg",
          linkUrl: "back_item_list.html",
          text: "售后",
          color: "#777777"
        }],
        column: 4,
      },
      ListData: {
        cells: [{
          iconPath: "https://image1.sansancloud.com/sansancloud/2018_9/11/14/41/49_530.jpg",
          linkUrl: "order_pintuan.html?payStatus=1&orderType=12",
          text: "我的拼单",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_10/9/14/16/9_477.jpg",
          linkUrl: "user_jifen_events.html",
          text: "我的积分",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_10/9/14/10/49_366.jpg",
          linkUrl: "my_coupons.html",
          text: "我的优惠券",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2019_1/4/11/7/54_534.jpg",
          linkUrl: "my_card_voucher.html",
          text: "我的卡券",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2019_1/11/20/49/36_852.jpg",
          linkUrl: "verification_record.html",
          text: "核销记录",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/54/39_501.jpg",
          linkUrl: "address.html",
          text: "收货地址",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/52/10_637.jpg",
          linkUrl: "yijian_fankui.html",
          text: "意见反馈",
          color: "#777777"
        }, {
          iconPath: "https://image1.sansancloud.com/xianhua/2018_9/5/16/45/22_284.jpg",
          linkUrl: "custom_page_about_us?pageNage=关于我们",
          text: "关于我们",
          color: "#777777"
        }],
        column: 3,
        showType: 0
      },
      serverData: {
        iconPath: "https://image1.sansancloud.com/jianzhan/2018_9/5/19/17/45_862.jpg?x-oss-process=style/preview_120",
        linkUrl: "mendian_center.html",
        text: "我是服务商",
        color: "#777777"
      },
      servantData: {
        iconPath: "https://image1.sansancloud.com/jianzhan/2018_9/5/19/17/45_862.jpg?x-oss-process=style/preview_120",
        linkUrl: "servant_center.html",
        text: "我是服务人员",
        color: "#777777"
      },
	  moreSelectType:[
		{
		  iconPath: "https://image1.sansancloud.com/jianzhan/2018_9/5/19/17/45_862.jpg?x-oss-process=style/preview_120",
		  linkUrl: "mendian_center.html",
		  text: "我是服务商",
		  typeText: 'mendian',
		  color: "#777777",
		  ownState: false,
		},{
		  iconPath: "https://image1.sansancloud.com/jianzhan/2018_9/5/19/17/45_862.jpg?x-oss-process=style/preview_120",
		  linkUrl: "servant_center.html",
		  text: "我是服务人员",
		  typeText:'servant',
		  color: "#777777",
		  ownState: false,
		}, {
		  iconPath: "https://image1.sansancloud.com/jianzhan/2018_9/5/19/17/45_862.jpg?x-oss-process=style/preview_120",
		  linkUrl: "servant_target_center.html",
		  text: "我是服务对象",
		  typeText: 'servantTarget',
		  color: "#777777",
		  ownState: false,
		}
	  ],
      posterState: false,
      ewmImgUrl: "",
      userInfo: ""
    };
  },

  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
	//#ifdef MP
    wx.getSetting({
      //检查用户是否授权了
      success(res) {
        console.warn("======检查用户是否授权了========", res);

        if (!res.authSetting['scope.userInfo']) {
          console.log('=====没授权===='); // that.showPopup = true

          that.setData({
            showPopup: true
          });
        } else {
          console.log('=====已授权====');
          that.dellSData();
          that.getSessionUserInfo();
          that.getParac();
        }
      }

    });
	//#endif
	//#ifdef APP-PLUS
		that.dellSData();
		that.getSessionUserInfo();
		that.getParac();
	//#endif
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  onShow: function () {
    let that = this;

    if (this.openShow) {
      wx.getSetting({
        //检查用户是否授权了
        success(res) {
          console.warn("======检查用户是否授权了========", res);
          if (!res.authSetting['scope.userInfo']) {
            console.log('=====没授权===='); // that.showPopup = true
            that.setData({
              showPopup: true
            });
          } else {
            console.log('=====已授权====');
          }
        }

      });
      this.setData({
        loginUser: getApp().globalData.loginUser
      });
      this.getSessionUserInfo();
    }

    this.openShow = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getSessionUserInfo();
    this.getParac();
    console.log(this.setting);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */

  /* 分享 */
  onShareAppMessage: function () {
    return getApp().globalData.shareForFx2(getApp().globalData.miniIndexPage);
  },
  methods: {
    // 关闭海报
    getChilrenPoster(e) {
      let that = this;
      that.setData({
        posterState: false
      });
    },

    clickWxGz: function () {
      let that = this;
      console.log('===bindWxGz====', that.loginUser);
      let loginUser = that.loginUser;
      let title = "你确定要绑定公众号推送嘛~";

      if (loginUser.platformUser.openid) {
        title = "你确定要解绑公众号推送嘛~";
      }

      wx.showModal({
        title: '提示',
        content: title,
        success: function (res) {
          if (res.confirm) {
            if (!loginUser.platformUser.openid) {
              that.bindWxGz();
            } else {
              that.unBindWxGz();
            }
          } else if (res.cancel) {}
        }
      });
    },
    bindWxGz: function () {
      let that = this;
      let paramsUrl = "https://mini.sansancloud.com/chainalliance/" + getApp().globalData.clientNo + "/bindWxGz.html?platformUserId=" + that.loginUser.platformUser.id;
      that.tolinkUrl(paramsUrl);
      that.getSessionUserInfo();
    },
    unBindWxGz: function () {
      let that = this;
      let customIndex = getApp().globalData.AddClientUrl("/wx_unbind_wx_gz_openid.html"); //拿custom_page

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log("====== res.data=========", res.data);
          wx.hideLoading();

          if (!res.data.errcode || res.data.errcode == '0') {
            wx.showToast({
              title: '解绑成功~',
              icon: 'success',
              duration: 1000
            });
            that.getSessionUserInfo();
          } else {
            console.log('加载失败');
            wx.showToast({
              title: res.data.errMsg + '~',
              image: '/images/icons/tip.png',
              duration: 1000
            });
          }
        },
        fail: function (res) {
          console.log(res);
          wx.hideLoading();
        }
      });
    },
    showPoster: function () {
      let that = this;
      console.log('===showPoster====', that.loginUser.id);

      if (that.loginUser && that.loginUser.platformUser.id) {
        let ewmImgUrl = getApp().globalData.getQrCode({
          type: "user_info",
          id: that.loginUser.platformUser.id
        });
        that.setData({
          posterState: true,
          ewmImgUrl: ewmImgUrl
        });
      } else {
        wx.showModal({
          title: '提示',
          content: '您还未登录，点击【确定】重新加载',
          success: function (res) {
            if (res.confirm) {
              that.getSessionUserInfo();
              this.getParac();
            } else if (res.cancel) {}
          }
        });
      }
    },
    getParac: function () {
      var that = this;
      var customIndex = getApp().globalData.AddClientUrl("/custom_page_userinfo.html", {}, 'get', '1'); //拿custom_page

      wx.request({
        url: customIndex.url,
        header: getApp().globalData.header,
        success: function (res) {
          console.log("====== res.data=========", res.data);
          wx.hideLoading();

          if (!res.data.errcode || res.data.errcode == '0') {
            that.setData({
              componentState: true
            });
          } else {
            console.log('加载失败');
            that.setData({
              componentState: false
            });
          }
        },
        fail: function (res) {
          console.log(res);
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '加载失败，点击【确定】重新加载',
            success: function (res) {
              if (res.confirm) {
                that.getParac();
              } else if (res.cancel) {
                getApp().globalData.toIndex();
              }
            }
          });
        }
      });
    },
    loginOut: function () {
      wx.navigateTo({
        url: '/pages/pre_change_user_info/index'
      });
    },
    login: function () {
      this.tolinkUrl("login.html")
      // wx.navigateTo({
      //   // url: '../login_wx/index',
      //   url: '/pagesTwo/login/index'
      // });
    },
    bindPickerChange: function (e) {
      let that = this;
      console.log("====bindPickerChange====", e);
      let index = e.detail.value;
      that.setData({
        curPersonnelType: index
      });
    },
    dellSData: function () {
		let that=this;
		let sendCustomPageData = JSON.stringify({
		  title: 'noTitle',
		  url: "userinfo"
		});
		that.setData({
		  sendCustomPageData: sendCustomPageData
		});
      this.setData({
        // headData: this.headData,
        blankData: this.blankData,
        orderData: this.orderData,
        ListData: this.ListData,
        serverData: this.serverData,
        servantData: this.servantData
      });
    },
    getSessionUserInfo: function () {
      var that = this;
      var postParamUserBank = getApp().globalData.AddClientUrl("/get_session_userinfo.html");
      wx.request({
        url: postParamUserBank.url,
        data: postParamUserBank.params,
        header: getApp().globalData.headerPost,
        success: function (res) {
          console.log(res.data);

          if (res.data.errcode == '0') {
            let UserInfo = res.data.relateObj.platformUser;
            let orderData = that.orderData;
            orderData.cells[0].showCountNum = UserInfo.unpayedCount;
            orderData.cells[1].showCountNum = UserInfo.unsendedCount;
            orderData.cells[2].showCountNum = UserInfo.unreceivedCount; // 11111

            let loginUser = res.data.relateObj;
            let curPersonnelType = that.curPersonnelType;
            let moreSelectType = that.moreSelectType;
            let havaMoreSelectType = [];
            let havaMoreSelectTypeArr = [];
            console.log("===moreSelectType===", moreSelectType);

            for (let i = 0; i < moreSelectType.length; i++) {
              if (loginUser.platformUser.managerMendianId && moreSelectType[i].typeText == 'mendian') {
                havaMoreSelectType.push(moreSelectType[i]);
              }

              if (loginUser.platformUser.managerServantId && moreSelectType[i].typeText == 'servant') {
                havaMoreSelectType.push(moreSelectType[i]);
              }

              if (loginUser.platformUser.managerServantTargetId && moreSelectType[i].typeText == 'servantTarget') {
                havaMoreSelectType.push(moreSelectType[i]);
              }
            }

            console.log("moreSelectType", moreSelectType, havaMoreSelectType);

            if (havaMoreSelectType.length != 0) {
              for (let i = 0; i < havaMoreSelectType.length; i++) {
                havaMoreSelectTypeArr.push(havaMoreSelectType[i].text); // 判断优先度

                if (loginUser.platformUser.managerMendianId && loginUser.platformUser.managerMendianId != 0 && havaMoreSelectType[i].typeText == 'mendian') {
                  console.log("服务商");
                  curPersonnelType = i;
                } else if (!loginUser.platformUser.managerMendianId && loginUser.platformUser.managerServantId && loginUser.platformUser.managerServantId != 0 && havaMoreSelectType[i].typeText == 'servant') {
                  console.log("服务人员");
                  curPersonnelType = i;
                } else if (!loginUser.platformUser.managerMendianId && !loginUser.platformUser.managerServantId && loginUser.platformUser.managerServantTargetId && loginUser.platformUser.managerServantTargetId != 0 && havaMoreSelectType[i].typeText == 'servantTarget') {
                  console.log("服务对象");
                  curPersonnelType = i;
                }
              }
            }

            console.log("curPersonnelType", curPersonnelType, havaMoreSelectTypeArr);
            that.setData({
              curPersonnelType: curPersonnelType,
              loginUser: loginUser,
              userInfo: getApp().globalData.userInfo,
              setting: getApp().globalData.setting,
              orderData: orderData,
              havaMoreSelectTypeArr: havaMoreSelectTypeArr,
              havaMoreSelectType: havaMoreSelectType
            }); // 11111

            getApp().globalData.loginUser = res.data.relateObj;
          } else {
            wx.showToast({
              title: res.data.errMsg,
              image: '/images/icons/tip.png',
              duration: 1000
            });
          }
        },
        fail: function (res) {
          console.log(res.data);
        },
        complete: function (res) {
          wx.stopPullDownRefresh();
        }
      });
    },
    bindGetUserInfo: function (e) {
      let that = this;
      that.setData({
        showPopup: false
      });
      console.log(that.showPopup);
      console.log(e.detail.userInfo);

      if (e.detail.userInfo) {
        //用户按了允许授权按钮
        console.log('用户按了允许授权按钮');
        that.dellSData();
        that.getSessionUserInfo();
        that.getParac();

        if (getApp().globalData.loginUser && getApp().globalData.loginUser.platformUser && !getApp().globalData.loginUser.platformUser.nickname) {
          getApp().globalData.sentWxUserInfo(getApp().globalData.loginUser);
        }
      } else {
        console.log('用户按了拒绝按钮'); //用户按了拒绝按钮
      }
    },
    cancel: function () {
      this.setData({
        showPopup: false
      });
    },

    /* 组件事件集合 */
    tolinkUrl: function (e) {
      let linkUrl = e.currentTarget ? e.currentTarget.dataset.link : e;
      getApp().globalData.linkEvent(linkUrl);
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>